<template>
	<view v-if="!isFirstload" class="container" :style="appThemeStyle">
		<!-- 页面头部 -->
		<view class="main-header"
			:style="{ height: platform == 'H5' ? '260rpx' : '300rpx', paddingTop: platform == 'H5' ? '0' : '130rpx' }">
			<!-- <image class="bg-image" src="/static/background/user-header2.png" mode="scaleToFill"></image> -->
			<image class="bg-image" src="/static/new/user/userBg.png" mode="scaleToFill"></image>
			<!-- 用户信息 -->
			<view v-if="isLogin" class="user-info">
				<view class="user-avatar" @click="$navTo('pages/user/personal/index')">
					<avatar-image :url="userInfo.avatar_url" :width="100" />
				</view>
				<view class="u-flex u-row-between" style="flex:1;">
					<view class="user-content">
						<!-- 会员昵称 -->
						<view class="nick-name oneline-hide" @click="$navTo('pages/user/personal/index')">
							{{ userInfo.nick_name }}
						</view>
						<!-- 会员等级 -->
						<view v-if="$checkModule('user-grade') && userInfo.grade_id > 0 && userInfo.grade"
							class="user-grade">
							<view class="user-grade_icon">
								<image class="image"
									src="">
								</image>
							</view>
							<view class="user-grade_name">
								<text>{{ userInfo.grade.name }}</text>
							</view>
						</view>
						<!-- 会员无等级时显示手机号 -->
						<view v-else class="mobile">{{ userInfo.mobile }}</view>
					</view>
					<view class="sign-in" @click="$navTo('pages/user/signIn/index')" v-if="is_sign_in == 1">
						{{isSign ? '已签到' :'签到'}}</view>
				</view>
			</view>
			<!-- 未登录 -->
			<view v-else class="user-info" @click="handleLogin">
				<view class="user-avatar">
					<avatar-image :width="100" />
				</view>
				<view class="user-content">
					<view class="nick-name">未登录</view>
					<view class="login-tips">点击登录账号</view>
				</view>
			</view>
		</view>

		<!-- 浆果币，优惠券 *新加 -->
		<view class="money u-flex u-row-between">
			<view class="mon-item" @click="$navTo('pages/wallet/index')">
				<view class="mon-text">浆果币</view>
				<view class="mon-num">{{ isLogin ? assets.balance : '--' }}</view>
				<image src="/static/new/user/user1.png" mode="widthFix"></image>
			</view>
			<view class="mon-item" @click="$navTo('pages/points/log')">
				<view class="mon-text">积分</view>
				<view class="mon-num">{{ isLogin ? assets.points : '--' }}</view>
				<image src="/static/new/user/user3.png" mode="widthFix"></image>
			</view>
			<view class="mon-item" @click="$navTo('pages/my-coupon/index')">
				<view class="mon-text">优惠券</view>
				<view class="mon-num">{{ isLogin ? assets.coupon : '--' }}</view>
				<image src="/static/new/user/user2.png" mode="widthFix"></image>
			</view>
		</view>

		<!-- 我的服务 -->
		<view class="my-service">
			<view class="service-title">更多服务</view>
			<view class="service-content clearfix">
				<block v-for="(item, index) in serviceList" :key="index">
					<view v-if="item.type == 'link' && (enabled == 1 ? true : item.id != 4)" class="service-item" @click="$navTo(item.url)">
						<view class="item-icon">
							<image :src="item.icon" mode="widthFix"></image>
						</view>
						<view class="item-name">{{ item.name }}</view>
					</view>
					<!-- 在线客服 -->
					<view v-if="item.type == 'contact'" class="service-item">
						<customer-btn>
							<view class="item-icon">
								<image :src="item.icon" mode="widthFix"></image>
							</view>
							<view class="item-name">{{ item.name }}</view>
						</customer-btn>
					</view>
				</block>
			</view>
		</view>

		<!-- 退出登录 -->
		<view v-if="isLogin" class="my-logout">
			<view class="logout-btn" @click="handleLogout()">
				<text>退出登录</text>
			</view>
		</view>

		<!-- 绑定手机号 -->
		<!-- 	<view v-if="isLogin && !userInfo.mobile && setting[SettingKeyEnum.REGISTER.value].isManualBind"
			class="my-mobile" @click="$navTo('pages/user/bind/index')">
			<view class="info">点击绑定手机号，确保账户安全</view>
			<view class="btn-bind">去绑定</view>
		</view> -->
	</view>
</template>

<script>
	import store from '@/store'
	import {
		inArray
	} from '@/utils/util'
	import AvatarImage from '@/components/avatar-image'
	import CustomerBtn from '@/components/customer-btn'

	import SettingKeyEnum from '@/common/enum/setting/Key'
	import SettingModel from '@/common/model/Setting'
	import * as UserApi from '@/api/user'
	import * as SettingApi from '@/api/setting'
	import * as SignApi from '@/api/signIn'
	import StoreApi from '@/api/store'
	import {
		checkLogin,
	} from '@/core/app'

	export default {
		components: {
			AvatarImage,
			CustomerBtn
		},
		data() {
			return {
				inArray,
				// 枚举类
				SettingKeyEnum,
				// 正在加载
				isLoading: true,
				// 首次加载
				isFirstload: true,
				// 是否已登录
				isLogin: false,
				// 系统设置
				setting: {},
				// 当前用户信息
				userInfo: {},
				// 账户资产
				assets: {
					balance: '--',
					points: '--',
					coupon: '--'
				},

				// 我的服务 
				// 	pages/address/index  收货地址
				// 	pages/coupon/index  领券中心
				// 	pages/my-coupon/index 优惠券
				// 	pages/points/log  我的积分
				serviceList: [{
						name: '浆果福利群',
						icon: '/static/new/user/service1.png',
						url: 'pages/index/crowd',
						type: 'link',
						id:1,
					},
					{
						name: '在线客服',
						icon: '/static/new/user/service2.png',
						type: 'contact',
						id:2,
					},
					{
						name: '关于我们',
						icon: '/static/new/user/service3.png',
						url: 'pages/help/index',
						type: 'link',
						id:3,
					},
					{
						name: '邀请有礼',
						icon: '/static/new/user/service4.png',
						url: 'pages/user/activity/index',
						type: 'link',
						id:4,
					},
				],
				// 1开启积分签到  0关闭积分签到
				is_sign_in: null,
				//当天是否签到
				isSign: null,
				// 活动是否开启(1开启 0关闭)
				enabled: null,
			}
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			this.onRefreshPage()
		},

		methods: {
			// 刷新页面
			onRefreshPage() {
				// 判断是否已登录
				this.isLogin = checkLogin()
				// 获取页面数据
				this.getPageData()
			},

			// 获取页面数据
			getPageData(callback) {
				const app = this
				app.isLoading = true
				Promise.all([app.getSetting(), app.getUserInfo(), app.getUserAssets(), app.getPoints(), app.getSignIn(), app.getStore()])
					.then(result => {
						app.isFirstload = false
						// 执行回调函数
						callback && callback()
					})
					.catch(err => console.log('catch', err))
					.finally(() => app.isLoading = false)
			},


			// 获取商城设置
			getSetting() {
				const app = this
				return new Promise((resolve, reject) => {
					SettingModel.data()
						.then(setting => {
							app.setting = setting
							resolve(setting)
						})
						.catch(reject)
				})
			},

			// 获取签到积分是否开启
			getPoints() {
				const app = this
				SettingApi.data()
					.then(result => {
						const res = result.data.setting.points
						app.is_sign_in = res.is_sign_in
					})
			},

			// 获取当天是否签和已签到天数
			getSignIn() {
				const app = this
				SignApi.list()
					.then(result => {
						app.isSign = result.data.isSign //是否签到
					})
			},
			
			
			// 获取商城信息
			getStore() {
				StoreApi.data().then(result => {
					this.enabled = result.data.setting.inviter_activity.enabled
				})
			},

			// 获取当前用户信息
			getUserInfo() {
				const app = this
				return new Promise((resolve, reject) => {
					!app.isLogin ? resolve(null) : UserApi.info({}, {
							load: app.isFirstload
						})
						.then(result => {
							app.userInfo = result.data.userInfo
							resolve(app.userInfo)
						})
						.catch(err => {
							if (err.result && err.result.status == 401) {
								app.isLogin = false
								resolve(null)
							} else {
								reject(err)
							}
						})
				})
			},

			// 获取账户资产
			getUserAssets() {
				const app = this
				return new Promise((resolve, reject) => {
					!app.isLogin ? resolve(null) : UserApi.assets({}, {
							load: app.isFirstload
						})
						.then(result => {
							app.assets = result.data.assets
							resolve(app.assets)
						})
						.catch(err => {
							if (err.result && err.result.status == 401) {
								app.isLogin = false
								resolve(null)
							} else {
								reject(err)
							}
						})
				})
			},

			// 跳转到登录页
			handleLogin() {
				!this.isLogin && this.$navTo('pages/login/index')
			},

			// 退出登录
			handleLogout() {
				const app = this
				uni.showModal({
					title: '友情提示',
					content: '您确定要退出登录吗?',
					success(res) {
						if (res.confirm) {
							store.dispatch('Logout', {})
								.then(result => app.onRefreshPage())
						}
					}
				})
			},
		},

		/**
		 * 下拉刷新
		 */
		onPullDownRefresh() {
			// 获取首页数据
			this.getPageData(() => {
				uni.stopPullDownRefresh()
			})
		},
	}
</script>

<style>
	page {
		background: #fff;
	}
</style>
<style lang="scss" scoped>
	.container {
		padding-bottom: 60rpx;
	}

	// 页面头部
	.main-header {
		// background-color: #FBF7EF;
		position: relative;
		width: 100%;
		background-size: 100% 100%;
		overflow: hidden;
		display: flex;
		align-items: center;
		padding-left: 30rpx;

		.bg-image {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			z-index: 0;
		}

		.user-info {
			width: 100%;
			display: flex;
			height: 100rpx;
			z-index: 1;
			padding-right: 30rpx;

			.user-content {
				display: flex;
				flex-direction: column;
				justify-content: center;
				margin-left: 30rpx;
				color: #fff;

				.nick-name {
					font-size: 34rpx;
					font-weight: bold;
					max-width: 300rpx;
				}

				.mobile {
					margin-top: 15rpx;
					font-size: 28rpx;
				}

				.user-grade {
					align-self: baseline;
					display: flex;
					align-items: center;
					background: #3c3c3c;
					margin-top: 12rpx;
					border-radius: 10rpx;
					padding: 4rpx 12rpx;

					.user-grade_icon .image {
						display: block;
						width: 32rpx;
						height: 32rpx;
					}

					.user-grade_name {
						margin-left: 5rpx;
						font-size: 26rpx;
						color: #EEE0C3;
					}

				}

				.login-tips {
					margin-top: 12rpx;
					font-size: 30rpx;
				}
			}

			.sign-in {
				font-size: 26rpx;
				background: #fff;
				padding: 11rpx 26rpx;
				border-radius: 10rpx;
			}
		}
	}

	// 角标组件
	.item-badge {
		position: absolute;
		top: 0;
		right: 55rpx;
		// background: $main-bg;
		background: #7ca934;
		color: #fff;
		border-radius: 100%;
		min-width: 38rpx;
		height: 38rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 1rpx;
		font-size: 24rpx;
	}

	// 浆果币,优惠券 *新加
	.money {
		width: 94%;
		margin: 0 auto;
		margin-top: 20rpx;

		.mon-item {
			width: 32%;
			height: 150rpx;
			border-radius: 10rpx;
			background: #f9f9f9;
			font-size: 26rpx;
			position: relative;
			padding: 15rpx;
			box-sizing: border-box;

			image {
				width: 65rpx;
				right: 10rpx;
				bottom: 10rpx;
				position: absolute;
			}

			.mon-text {
				color: #616161;
				margin-bottom: 10rpx;
			}

			.mon-num {
				font-weight: bold;
				font-size: 32rpx;
			}
		}
	}


	// 我的服务
	.my-service {
		margin: 22rpx auto 22rpx auto;
		padding: 22rpx 0;
		width: 94%;
		border-radius: 10rpx;
		background: #f9f9f9;

		.service-title {
			padding-left: 24rpx;
			margin-bottom: 20rpx;
			font-size: 30rpx;
		}

		.service-content {
			margin-bottom: -20rpx;

			.service-item {
				position: relative;
				width: 25%;
				float: left;
				margin-bottom: 30rpx;

				.item-icon {
					text-align: center;
					margin: 0 auto;
					padding: 14rpx;
					// color: $main-bg;

					image {
						width: 50rpx;
					}
				}

				.item-name {
					font-size: 26rpx;
					color: #545454;
					text-align: center;
				}
			}
		}
	}

	// 退出登录
	.my-logout {
		display: flex;
		justify-content: center;
		margin-top: 50rpx;

		.logout-btn {
			width: 94%;
			margin: 0 auto;
			font-size: 28rpx;
			color: #616161;
			border-radius: 20rpx;
			border: 1px solid #dcdcdc;
			padding: 23rpx 0;
			text-align: center;
		}
	}



	// 绑定手机号 样式1
	// .my-mobile {
	// 	display: flex;
	// 	justify-content: space-between;
	// 	align-items: center;
	// 	padding: 16rpx 40rpx;
	// 	background: #FCEBD1;

	// 	.info {
	// 		color: #cd8c0c;
	// 		font-size: 28rpx;
	// 	}

	// 	.btn-bind {
	// 		padding: 8rpx 24rpx;
	// 		background-color: #EAB766;
	// 		color: #fff;
	// 		border-radius: 30rpx;
	// 		font-size: 26rpx;
	// 		text-align: center;
	// 	}
	// }
</style>